﻿Imports System.Data.SqlClient
Imports DTO

Public Class LoaiDAO

    Public Function LayID(ByVal id As Integer) As LoaiDTO
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaLoai", id)
        Dim dt = cn.ExecuteQuery("LayLoaiTheoID", para)
        Dim loai As New LoaiDTO()
        If dt.Rows.Count <> 0 Then
            loai.MaLoai = Integer.Parse(dt.Rows(0).Item("MaLoai").ToString())
            loai.TenLoai = dt.Rows(0).Item("TenLoai").ToString()
        End If
        Return loai
    End Function

    Public Function LayTatCa() As IList
        Dim cn As New DataProvider()
        Dim array As New ArrayList()
        Dim dt = cn.ExecuteQuery("LayAllLoai")
        Dim loai As New LoaiDTO()
        If dt.Rows.Count <> 0 Then
            For Each dr As DataRow In dt.Rows
                loai.MaLoai = Integer.Parse(dr.Item("MaLoai").ToString())
                loai.TenLoai = dr.Item("TenLoai").ToString()
                array.Add(loai)
            Next
        End If
        Return array
    End Function


    Public Function LayTatCaTT() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("LayAllLoai")
    End Function



    Public Sub ThemLoai(ByVal loai As LoaiDTO)
        Dim cn As New DataProvider()
        Dim param As SqlParameter() = New SqlParameter(1) {}
        param(0) = New SqlParameter("@TenLoai", loai.TenLoai)
        cn.ExecuteNonQuery("InsertLoai", param)
    End Sub

    Public Sub CapNhapLoai(ByVal loai As LoaiDTO)
        Dim cn As New DataProvider()
        Dim param As SqlParameter() = New SqlParameter(2) {}
        param(0) = New SqlParameter("@MaLoai", loai.MaLoai)
        param(1) = New SqlParameter("@TenLoai", loai.TenLoai)
        cn.ExecuteNonQuery("CapNhapLoai", param)
    End Sub


    Public Sub XoaLoai(ByVal id As Integer)
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaLoai", id)
        cn.ExecuteNonQuery("DeleteLoai", para)
    End Sub


End Class
